﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace NetJsWire.Widgets.Config
{
    public class NotQuotedJsonStringConverter : JsonConverter
    {
        #region Overrides of JsonConverter

        /// <summary>
        ///   Writes the JSON representation of the object.
        /// </summary>
        /// <param name = "writer">The <see cref = "T:Newtonsoft.Json.JsonWriter" /> to write to.
        /// </param>
        /// <param name = "value">The value.
        /// </param>
        /// <param name = "serializer">The calling serializer.</param>
        public override void WriteJson( JsonWriter writer, object value, JsonSerializer serializer )
        {
            if ( value is string )
            {
                if ( !string.IsNullOrWhiteSpace( value.ToString() ) )
                    writer.WriteRawValue( value.ToString().Replace( Environment.NewLine, writer.Formatting == Formatting.None ? "" : "\n" ).Replace( "\t", "" ) );
                else
                    writer.WriteNull();
            }
            else if ( value is Dictionary<string, string> )
            {
                var dict = value as Dictionary<string, string>;
                writer.WriteStartObject();
                foreach ( var pair in dict )
                {
                    writer.WritePropertyName( pair.Key );
                    writer.WriteRawValue( pair.Value );
                }
                writer.WriteEndObject();
            }
            else if ( value is IList<string> )
            {
                writer.WriteStartArray();
                var list = value as IList<string>;
                foreach ( var li in list )
                {
                    writer.WriteRawValue( li );
                }
                writer.WriteEndArray();
            }
            else if ( value is IList<object> )
            {
                writer.WriteStartArray();
                var list = value as IList<object>;
                foreach ( var li in list )
                {
                    writer.WriteRawValue( JsonConvert.SerializeObject( li ) );
                }
                writer.WriteEndArray();
            }
            else
            {
                writer.WriteRawValue( JsonConvert.SerializeObject( value ) );
            }
        }

        /// <summary>
        ///   Reads the JSON representation of the object.
        /// </summary>
        /// <param name = "reader">The <see cref = "T:Newtonsoft.Json.JsonReader" /> to read from.</param>
        /// <param name = "objectType">Type of the object.</param>
        /// <param name = "existingValue">The existing value of object being read.</param>
        /// <param name = "serializer">The calling serializer.</param>
        /// <returns>
        ///   The object value.
        /// </returns>
        public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer )
        {
            throw new NotImplementedException();
        }

        /// <summary>
        ///   Determines whether this instance can convert the specified object type.
        /// </summary>
        /// <param name = "objectType">Type of the object.</param>
        /// <returns>
        ///   <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
        /// </returns>
        public override bool CanConvert( Type objectType )
        {
            return objectType == typeof( string ) || objectType == typeof( IDictionary<string, string> ) || objectType == typeof( IList<string> ) || objectType == typeof( IList<object> ) || objectType == typeof( object );
        }

        #endregion
    }
}
